package com.example.eback.service;

import com.example.eback.dao.FilesDAO;
import com.example.eback.result.MeasurementData;
import com.example.eback.result.ProcessResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;


import java.util.Map;

@Service
public class FileProcessService {

    @Autowired
    FilesDAO filesDAO;

    /**
     * 访问flask分割接口
     * @param uploadFileUrl
     * @return
     */
    public ProcessResponse FileProcess(String uploadFileUrl){
        RestTemplate restTemplate = new RestTemplate();
        String flaskUrl = "http://10.134.48.5:5000/processvnet";
        //将url封装起来
        Map<String, String> data = new HashMap<>();
        data.put("fileUrl", uploadFileUrl);

        //访问flask端口
        ProcessResponse response=restTemplate.postForObject(flaskUrl, data, ProcessResponse.class);
        return response;
    }

    /**
     * 访问flask标签数量统计接口
     * @param uploadFileUrl
     * @return
     */
    public String LabelNumProcess(String uploadFileUrl){
        RestTemplate restTemplate = new RestTemplate();
        String flaskUrl = "http://10.134.48.5:5000/labelnum";
        //将url封装起来
        Map<String, String> data = new HashMap<>();
        data.put("fileUrl", uploadFileUrl);

        //访问flask端口
        String num=restTemplate.postForObject(flaskUrl, data, String.class);
        return num;
    }

    /**
     * 访问flask的数据分析接口
     * @param OriginFileUrl
     * @param LabelFileUrl
     * @return
     */
    //数据分析
    public MeasurementData Measurement(String OriginFileUrl,String LabelFileUrl){
        RestTemplate restTemplate = new RestTemplate();
        String flaskUrl = "http://10.134.48.5:5000/measurement";
        //将url封装起来
        Map<String, String> data = new HashMap<>();
        data.put("OriginFileUrl", OriginFileUrl);
        data.put("LabelFileUrl", LabelFileUrl);

        //访问flask端口
        MeasurementData measurementData=restTemplate.postForObject(flaskUrl, data, MeasurementData.class);
        return measurementData;
    }


    /**
     * 计算两器官的距离
     */
    public double CalculateDistance(String OriginFileUrl,String LabelFileUrl,int label_one,int label_two){
        RestTemplate restTemplate = new RestTemplate();
        String flaskUrl = "http://10.134.48.5:5000/calculate_distance";
        //将url封装起来
        Map<String, String> data = new HashMap<>();
        data.put("image_file_url", OriginFileUrl);
        data.put("segmentation_file_url", LabelFileUrl);
        data.put("organ1_label", Integer.toString(label_one));
        data.put("organ2_label", Integer.toString(label_two));
        //访问flask端口
        double distance=restTemplate.postForObject(flaskUrl, data, Double.class);
        return distance;
    }


}
